package com.umeox.quranplayer.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.quranplayer.R
import com.umeox.quranplayer.repository.*

class MusicGroupActivity : ThemeAppCompatActivity(), View.OnClickListener {

    private var llListQuran: LinearLayout? = null
    private var llListFavorite: LinearLayout? = null
    private var llListMorning: LinearLayout? = null
    private var llListNight: LinearLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_music_group)
        initView()
        initListener()
    }


    private fun initView() {
        llListQuran = findViewById(R.id.ll_list_quran)
        llListFavorite = findViewById(R.id.ll_list_favorite)
        llListMorning = findViewById(R.id.ll_list_morning)
        llListNight = findViewById(R.id.ll_list_night)
    }

    private fun initListener() {
        llListQuran?.setOnClickListener(this)
        llListFavorite?.setOnClickListener(this)
        llListMorning?.setOnClickListener(this)
        llListNight?.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_list_quran -> {
                startActivityForResult(Intent(this, QuranListActivity::class.java),
                   QURAN_LIST_REQUEST_CODE
                )
            }
            R.id.ll_list_favorite -> {
                startActivityForResult(Intent(this, FavoriteListActivity::class.java),
                    QURAN_LIST_REQUEST_CODE
                )
            }
            R.id.ll_list_morning -> {
                setResult(Activity.RESULT_OK,Intent().apply {
                    putExtra(INTENT_KEY_LIST_TYPE,LIST_TYPE_MORNING)
                    putExtra(INTENT_KEY_AUDIO_NAME,MORNING_QURAN_FILE_NAME)
                })
                finish()
            }
            R.id.ll_list_night -> {
                setResult(Activity.RESULT_OK,Intent().apply {
                    putExtra(INTENT_KEY_LIST_TYPE,LIST_TYPE_NIGHT)
                    putExtra(INTENT_KEY_AUDIO_NAME,NIGHT_QURAN_FILE_NAME)
                })
                finish()
            }
            else -> {
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == QURAN_LIST_REQUEST_CODE) {
            data?.let {
                val playListType = it.getIntExtra(INTENT_KEY_LIST_TYPE, 0)
                val playName = it.getStringExtra(INTENT_KEY_AUDIO_NAME)?:""
                setResult(Activity.RESULT_OK,Intent().apply {
                    putExtra(INTENT_KEY_LIST_TYPE,playListType)
                    putExtra(INTENT_KEY_AUDIO_NAME,playName)
                })
                finish()
            }
        }
    }
}